package com.ktjiaoyu.crm.repository;

import com.ktjiaoyu.crm.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
    @Query(value = "select * from sys_user where usr_role_id=?1", nativeQuery = true)
    public List<User> findByRoleId(Long roleId);

    //    @Query("select u from User u where u.usrRoleId=:roleId")
//    public List<User> findByRoleId(@Param("roleId") Long roleId);
    @Transactional(timeout = 10)
    @Modifying
    @Query("update User u set u.usrName=?1 where u.usrId=?2")
    public int modifyNameById(String usrName, Long usrId);

    //此方法对应User实体中的命名查询
    //如果希望根据接口方法名来实现数据库操作，则应命名为：findUsersByUsrName
    public List<User> findUsersByName(String usrName);

    @Query("select u from User u where u.usrRoleId=?1")
    public Page<User> findPageByUsrRoleId(Long roleId, Pageable pageable);

}
